/**
 * Created by YY on 16/8/17.
 */
angular.module('BatchBind')
    .controller('BatchBind.MODetailCtrl', ['$scope', '$state', '$filter', 'Http', 'BatchBind.service',
        function ($scope, $state, $filter, Http, bindService) {
            $scope.data = {};

            var completePlanRecords;
            Http.post({
                url: '/api/barcode/getMOLotNumber'
            }).then(function (results) {
                angular.forEach(results, function (result) {
                    result.CompleteTime = dateFormat(result.CompleteTime, "yyyy-MM-dd hh:mm:ss");
                });
                completePlanRecords = results;
                $scope.data.records = angular.copy(results);
            });

            $scope.search = function (queryStr) {
                if (queryStr) {
                    $scope.data.records = $filter('filter')(completePlanRecords, function (record) {
                        var reg = new RegExp(queryStr);
                        if (record.ItemMaster && record.ItemMaster.Code) {
                            return reg.test(record.ItemMaster.Code) || reg.test(record.LotNumber);
                        } else {
                            return reg.test(record.LotNumber);
                        }
                    });
                } else {
                    $scope.data.records = angular.copy(completePlanRecords);
                }
            };
            $scope.selectObj = function (record) {
                bindService.setLotNumber(record);
                $state.go('BatchBind');
            };

            function dateFormat(dateStr, fmt) { //author: meizz 
                var date = new Date(dateStr);
                var o = {
                    "M+": date.getMonth() + 1, //月份   
                    "d+": date.getDate(), //日   
                    "h+": date.getHours(), //小时   
                    "m+": date.getMinutes(), //分   
                    "s+": date.getSeconds(), //秒   
                    "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
                    "S": date.getMilliseconds() //毫秒   
                };
                if (/(y+)/.test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
                }
                for (var k in o) {
                    if (new RegExp("(" + k + ")").test(fmt)) {
                        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                    }
                }
                return fmt;
            }
        }
    ]);